package com.mtk.launcher.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.SeekBar;

import androidx.annotation.Nullable;

import com.lark.wifi.util.MyLog;
import com.mtk.launcher.databinding.ActivitySleepBinding;
import com.mtk.launcher.listener.ObjectClickListener;
import com.mtk.launcher.power.util.CheckTimeUtil;
import com.mtk.launcher.service.MyService;
import com.mtk.launcher.util.ClickSimulator;
import com.mtk.launcher.util.ScreenWakeUtil;
import com.mtk.launcher.util.TimerDealUtil;
import com.mtk.launcher.util.shared.SharedPerManager;
import com.mtk.launcher.view.ButtonGenerate;
import com.mtk.launcher.view.LayoutViewBase;
import com.mtk.launcher.view.TimeCardGenerate;

/***
 * 休眠界面
 */
public class SleepActivity extends BaseActivity {

    ActivitySleepBinding binding;

    public static final String EXIT_SLEEP_MODEL = "com.mtk.launcher.activity.EXIT_SLEEP_MODEL";
    public static final String ENTER_SLEEP_MODEL = "com.mtk.launcher.activity.ENTER_SLEEP_MODEL";

    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(EXIT_SLEEP_MODEL)) {
                finish();
            } else if (action.equals(ENTER_SLEEP_MODEL)) {
                //防止其他情况亮屏
                ScreenWakeUtil.getInstance(SleepActivity.this).gotoSleep();
            }
        }
    };


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivitySleepBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        ScreenWakeUtil.getInstance(SleepActivity.this).gotoSleep();
        initView();
        initReceive();
        initTestClick();
    }

    LayoutViewBase layoutViewTimeClock;
    int x = 0;
    int y = 0;
    int addNum = 0;

    private void initTestClick() {
        binding.btnTest.post(new Runnable() {
            @Override
            public void run() {
                int width = binding.btnTest.getWidth();
                int height = binding.btnTest.getHeight();
                x = width / 2;
                y = height / 2;
            }
        });
        binding.btnTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyLog.test("模拟点击的日志输出");
                dealCurrentScreenStatues();
            }
        });
        layoutViewTimeClock = new LayoutViewBase() {
            @Override
            public View getView() {
                return null;
            }

            @Override
            public void clearMemory() {

            }

            @Override
            public void timeChangeToUpdateView() {
                addNum++;
                if (addNum % 15 != 0) {
                    return;
                }
                if (x == 0 || y == 0) {
                    return;
                }
                MyLog.test("模拟点击的行为开始");
                ClickSimulator.simulateClick(binding.btnTest, x, y);
            }
        };
        TimerDealUtil.getInstance().addGeneratorToList(layoutViewTimeClock);
    }

    LayoutViewBase layoutViewTime;

    private void initView() {
        int vieWidth = 800;
        int viewHeight = 250;
        int x = SharedPerManager.getScreenWidth() / 2 - vieWidth / 2;
        int y = Math.min(SharedPerManager.getScreenWidth(), SharedPerManager.getScreenHeight()) / 5;
        layoutViewTime = new TimeCardGenerate(SleepActivity.this, x, y, vieWidth, viewHeight);
        binding.relaBgg.addView(layoutViewTime.getView(), layoutViewTime.getLayoutParams());

        int screenWidth = SharedPerManager.getScreenWidth();
        int screenHeigh = SharedPerManager.getScreenHeight();
        int viewSize = 100;

        int viewX = screenWidth / 2 - viewSize / 2;
        int viewY = screenHeigh - viewSize - 50;

        LayoutViewBase layoutButton = new ButtonGenerate(SleepActivity.this, viewX, viewY, viewSize, viewSize);
        binding.relaBgg.addView(layoutButton.getView(), layoutButton.getLayoutParams());
        layoutButton.setClickViewBack(new ObjectClickListener() {
            @Override
            public void clickView() {
                startActivity(new Intent(SleepActivity.this, MainActivity.class));
                finish();
            }
        });
    }

    private void initReceive() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(EXIT_SLEEP_MODEL);
        filter.addAction(ENTER_SLEEP_MODEL);
        registerReceiver(broadcastReceiver, filter);
    }

    public static boolean isViewForst = false;

    @Override
    protected void onResume() {
        super.onResume();
        isViewForst = true;
    }

    @Override
    protected void onStop() {
        super.onStop();
        isViewForst = false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e("cdlLog", "==onStop==wakeUpScreen");
        ScreenWakeUtil.getInstance(SleepActivity.this).wakeUpScreen();
        if (broadcastReceiver != null) {
            unregisterReceiver(broadcastReceiver);
        }
        if (layoutViewTime != null) {
            layoutViewTime.clearMemory();
        }
        TimerDealUtil.getInstance().removeGeneratorToList(layoutViewTimeClock);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private long lastClickTime = 0;

    /***
     * 检查当前是亮屏还是暗屏状态
     */
    private void dealCurrentScreenStatues() {
        long currentTime = System.currentTimeMillis();
        long oneMin = 1000 * 60;
        if (currentTime - lastClickTime < oneMin) {
            MyLog.powerOnOff("检查当前是否是开机时间=SleepActivity=时间太短拦截");
            return;
        }
        lastClickTime = currentTime;
        boolean isOnTime = CheckTimeUtil.checkCurrentIsOnOrOffTime();
        MyLog.powerOnOff("检查当前是否是开机时间=SleepActivity=" + isOnTime);
        if (isOnTime) {
            startActivity(new Intent(SleepActivity.this, MainActivity.class));
            finish();
        }
    }
}
